# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.jsonUtils_v2.table2Json_Zhao import Table2JsonZhao
from com.zzh.utils.requestFieldMapDBField import fieldMap_Zhao

class FavoriteCartDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertFavoriteCart(self, info):
        """
        添加 favorite_cart 信息
        :param info:
        :return:
        """
        userId = info['userId']
        itemId = info['itemId']
        isLock = info['isLock']

        sql = "insert into zzh_favorite_cart (user_id, item_id, is_lock) VALUES " \
              "(%s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, itemId, isLock])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findFavorById(self, favorId):
        """
        根据ID查询 favorite_cart 信息
        :param favorId:
        :return:
        """
        sql = "select * from zzh_favorite_cart WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [favorId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.favorite_cart_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findFavorByCondition(self, **kwargs):
        """
        zzh_favorite_cart 表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Zhao.viewFavorCartItemFieldMap2
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_favor_cart_item2 WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_favor_cart_item2 WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2JsonZhao.view_favor_cart_item_list_to_json(lists)
            return adJson, rs
        cur.close()
        self.conn.close()
        return jsons, 0

    def deleteFavort(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_favorite_cart WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateFavor(self, info):
        """
        更新 favorite_cart 表
        :param info:
        :return:
        """
        mId = info['id']
        userId = info['userId']
        itemId = info['itemId']
        isLock = info['isLock']

        sql = "update zzh_favorite_cart set user_id=%s,item_id=%s, is_lock=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, itemId, isLock, mId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectFavor(self, favorId):
        """
        根据ID查询，返回元组
        :param favorId:
        :return: data tuple
        """
        sql = "select * from zzh_favorite_cart WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [favorId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def addFavoriteCart(self, data):
        """
        娣诲姞 favorite_cart 淇℃伅
        :param data:
        :return:
        """
        userId = data['userId']
        itemId = data['itemId']
        isLock = data['isLock']

        cur = self.conn.cursor()
        for item in itemId:
            sql = "insert into zzh_favorite_cart (user_id, item_id, is_lock) VALUES " \
                  "(%s, %s, %s)"
            rs = cur.execute(sql, [userId, item, isLock])
            self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def removeFavorCart(self, data):
        """
        鎵归噺绉婚櫎鏀惰棌杞?
        :param data:
        :return:
        """

        userId = data['userId']
        itemId = data['itemId']
        isLock = data['isLock']
        cur = self.conn.cursor()
        for item in itemId:
            sql = "update zzh_favorite_cart set is_lock=%s WHERE user_id=%s and item_id=%s"
            rs = cur.execute(sql, [isLock, userId, item])
            self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

